home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_draw.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-16  |  5.4 KB  |  205 lines

  1. //
  2. // "$Id: fl_draw.cxx,v 1.6.2.1 1999/06/16 15:14:42 mike Exp $"
  3. //
  4. // Label drawing code for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Implementation of fl_draw(const char*,int,int,int,int,Fl_Align)
  27. // Used to draw all the labels and text, this routine:
  28. // Word wraps the labels to fit into their bounding box.
  29. // Breaks them into lines at the newlines.
  30. // Expands all unprintable characters to ^X or \nnn notation
  31. // Aligns them against the inside of the box.
  32.  
  33. #include <FL/fl_draw.H>
  34. #include <string.h>
  35.  
  36. #define MAXBUF 1024
  37.  
  38. char fl_draw_shortcut;    // set by fl_labeltypes.C
  39. static char* underline_at;
  40.  
  41. // Copy p to buf, replacing unprintable characters with ^X and \nnn
  42. // Stop at a newline of if MAXBUF characters written to buffer.
  43. // Also word-wrap if width exceeds maxw.
  44. // Returns a pointer to the start of the next line of caharcters.
  45. // Sets n to the number of characters put into the buffer.
  46. // Sets width to the width of the string in the current font.
  47.  
  48. static const char*
  49. expand(const char* from, char* buf, double maxw, int& n, double &width, int wrap) {
  50.  
  51.   char* o = buf;
  52.   char* e = buf+(MAXBUF-4);
  53.   underline_at = 0;
  54.   char* word_end = o;
  55.   const char* word_start = from;
  56.   double w = 0;
  57.  
  58.   const char* p = from;
  59.   for (;; p++) {
  60.  
  61.     int c = *p & 255;
  62.  
  63.     if (!c || c == ' ' || c == '\n') {
  64.       // test for word-wrap:
  65.       if (word_start < p && wrap) {
  66.     double newwidth = w + fl_width(word_end, o-word_end);
  67.     if (word_end > buf && newwidth > maxw) { // break before this word
  68.       o = word_end;
  69.       p = word_start;
  70.       break;
  71.     }
  72.     word_end = o;
  73.     w = newwidth;
  74.       }
  75.       if (!c) break;
  76.       else if (c == '\n') {p++; break;}
  77.       word_start = p+1;
  78.     }
  79.  
  80.     if (o > e) break; // don't overflow buffer
  81.  
  82.     if (c == '\t') {
  83.       for (c = (o-buf)%8; c<8 && o<e; c++) *o++ = ' ';
  84.  
  85.     } else if (c == '&' && fl_draw_shortcut && *(p+1)) {
  86.       if (*(p+1) == '&') {p++; *o++ = '&';}
  87.       else if (fl_draw_shortcut != 2) underline_at = o;
  88.  
  89.     } else if (c < ' ' || c == 127) { // ^X
  90.       *o++ = '^';
  91.       *o++ = c ^ 0x40;
  92.  
  93.    /*
  94.     * mike@easysw.com - The following escaping code causes problems when
  95.     * using the PostScript ISOLatin1 and WinANSI encodings, because these
  96.     * map to I18N characters...
  97.     */
  98. #if 0
  99.     } else if (c >= 128 && c < 0xA0) { // \nnn
  100.       *o++ = '\\';
  101.       *o++ = (c>>6)+'0';
  102.       *o++ = ((c>>3)&7)+'0';
  103.       *o++ = (c&7)+'0';
  104. #endif /* 0 */
  105.  
  106.     } else if (c == 0xA0) { // non-breaking space
  107.       *o++ = ' ';
  108.  
  109.     } else {
  110.       *o++ = c;
  111.  
  112.     }
  113.   }
  114.  
  115.   width = w + fl_width(word_end, o-word_end);
  116.   *o = 0;
  117.   n = o-buf;
  118.   return p;
  119. }
  120.  
  121. void fl_draw(
  122.     const char* str,    // the (multi-line) string
  123.     int x, int y, int w, int h,    // bounding box
  124.     Fl_Align align,
  125.     void (*callthis)(const char*,int,int,int)
  126. ) {
  127.   const char* p;
  128.   const char* e;
  129.   char buf[MAXBUF];
  130.   int buflen;
  131.  
  132.   // count how many lines and put the last one into the buffer:
  133.   int lines;
  134.   double width;
  135.   for (p=str,lines=0; ;) {
  136.     e = expand(p, buf, w, buflen, width, align&FL_ALIGN_WRAP);
  137.     lines++;
  138.     if (!*e) break;
  139.     p = e;
  140.   }
  141.  
  142.   // figure out vertical position of the first line:
  143.   int ypos;
  144.   int height = fl_height();
  145.   if (align & FL_ALIGN_BOTTOM) ypos = y+h-(lines-1)*height;
  146.   else if (align & FL_ALIGN_TOP) ypos = y+height;
  147.   else ypos = y+(h-lines*height)/2+height;
  148.  
  149.   // now draw all the lines:
  150.   int desc = fl_descent();
  151.   for (p=str; ; ypos += height) {
  152.     if (lines>1) e = expand(p, buf, w, buflen, width, align&FL_ALIGN_WRAP);
  153.  
  154.     int xpos;
  155.     if (align & FL_ALIGN_LEFT) xpos = x;
  156.     else if (align & FL_ALIGN_RIGHT) xpos = x+w-int(width+.5);
  157.     else xpos = x+int((w-width)/2);
  158.  
  159.     callthis(buf,buflen,xpos,ypos-desc);
  160.  
  161.     if (underline_at)
  162.       callthis("_",1,xpos+int(fl_width(buf,underline_at-buf)),ypos-desc);
  163.  
  164.     if (!*e) break;
  165.     p = e;
  166.   }
  167.  
  168. }
  169.  
  170. void fl_draw(
  171.   const char* str,    // the (multi-line) string
  172.   int x, int y, int w, int h,    // bounding box
  173.   Fl_Align align) {
  174.   if (!str || !*str) return;
  175.   if (w && h && !fl_not_clipped(x, y, w, h)) return;
  176.   if (align & FL_ALIGN_CLIP) fl_clip(x, y, w, h);
  177.   fl_draw(str, x, y, w, h, align, fl_draw);
  178.   if (align & FL_ALIGN_CLIP) fl_pop_clip();
  179. }
  180.  
  181. void fl_measure(const char* str, int& w, int& h) {
  182.   h = fl_height();
  183.   if (!str || !*str) {w = 0; return;}
  184.   const char* p;
  185.   const char* e;
  186.   char buf[MAXBUF];
  187.   int buflen;
  188.   int lines;
  189.   double width;
  190.   int W = 0;
  191.   for (p=str,lines=0; ;) {
  192.     e = expand(p, buf, w, buflen, width, w!=0);
  193.     if (int(width) > W) W = int(width);
  194.     lines++;
  195.     if (!*e) break;
  196.     p = e;
  197.   }
  198.   w = W;
  199.   h = lines*h;
  200. }
  201.  
  202. //
  203. // End of "$Id: fl_draw.cxx,v 1.6.2.1 1999/06/16 15:14:42 mike Exp $".
  204. //
  205.